package com.log.compone;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author zhaoYi
 * @Date 2024/7/14 10:18
 */
public class FileOpsTest {

    private static final String FILE_PATH = "C:\\Users\\MissZhou\\Desktop\\testFiles\\";


    public static void main(String[] args) throws IOException {


        //写入文件
        Map<String, Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("age",35);
        map.put("gender",0);
        String jsonString = JSON.toJSONString(map);
        Files.write(Paths.get(FILE_PATH+"test.log"), jsonString.getBytes(StandardCharsets.UTF_8));

        //读取文件
        Path path = Paths.get(FILE_PATH+"test.log");
        byte[] bytes = Files.readAllBytes(path);
        String s = new String(bytes);
        JSONObject jsonObject = JSON.parseObject(s);
        System.out.println(jsonObject);
    }
}
